package org.dromara.system.business.learning.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.domain.R;
import org.dromara.system.business.common.PageVo;
import org.dromara.system.business.learning.dto.LearningCourseDto;
import org.dromara.system.business.learning.entity.LearningCourse;
import org.dromara.system.business.learning.select.LearningSelect;
import org.dromara.system.business.learning.service.LearningCourseService;
import org.dromara.system.enums.BussinessTableEnum;
import org.dromara.system.service.BusinessFileRelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * (LearningCourse)表控制层
 *
 * @author zms
 * @since 2025-02-07 14:42:22
 */
@RestController
@RequestMapping("learningCourse")
public class LearningCourseController {
    /**
     * 服务对象
     */
    @Autowired
    private LearningCourseService learningCourseService;

    @Autowired
    private BusinessFileRelService businessFileRelService;

    /**
     * 分页查询所有数据
     *
     * @param pageVo 分页对象
     * @param learningSelect 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(PageVo pageVo, LearningSelect learningSelect) {
        Page<LearningCourse> pages = learningCourseService.selectPage(pageVo, learningSelect);

        return R.ok(pages);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable String id) {
        LearningCourse learningCourse = learningCourseService.getById(id);
        return R.ok(learningCourse);
    }

    /**
     * 新增数据
     *
     * @param learningCourseDto 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody LearningCourseDto learningCourseDto) {
        Boolean b =  learningCourseService.saveInfo(learningCourseDto);
        if (b){
            return R.ok("新增成功");
        }else {
            return R.warn("新增失败");
        }
    }

    /**
     * 修改数据
     *
     * @param learningCourseDto 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody LearningCourseDto learningCourseDto) {
        boolean b = learningCourseService.updateInfo(learningCourseDto);
        return R.ok(b);
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public R delete(@PathVariable String id) {
        LearningCourse byId = learningCourseService.getById(id);
        if (byId != null && byId.getState().equals("0")){
            boolean b = learningCourseService.removeById(id);
            businessFileRelService.fileMesDelete(Long.valueOf(id), BussinessTableEnum.LEARNING_COURSE.getValue());
            return R.ok(b);
        }else {
            return R.warn("该课程上架中，无法删除");
        }
    }

    /**
     * 上下架
     */
    @PutMapping("{id}/{state}")
    public R updateState(@PathVariable String id, @PathVariable String state) {
        LearningCourse learningCourse = learningCourseService.getById(id);
        learningCourse.setState(state);
        boolean b = learningCourseService.updateById(learningCourse);
        return R.ok(b);
    }
}

